<template>

  <div>

    <div style="display: flex; padding: 10px;">
      <div >
        <h1> 请输入需要生成的 Xmind 文本</h1>
        <el-input
            v-model="textarea"
            style="width: 600px;height:500px;margin-top: 20px;"
            type="textarea"
            placeholder="Please input"
        />

      </div>

      <div style="padding: 10px">
        <div style="display: flex; padding: 10px;width: 800px">
          <div style="margin-right: 10px"><el-switch v-model="horizontal"></el-switch> 横向</div>
          <!--          默认可收起-->
                    <div style="margin-right: 10px"><el-switch v-model="collapsable"></el-switch> 可收起</div>
          <!--          <div style="margin-right: 10px"><el-switch v-model="disaled"></el-switch> 禁止编辑</div>-->
          <!--          <div style="margin-right: 10px"><el-switch v-model="onlyOneNode"></el-switch> 仅拖动当前节点</div>-->
          <!--          <div style="margin-right: 10px"><el-switch v-model="cloneNodeDrag"></el-switch> 拖动节点副本</div>-->
          <div style="margin-right: 10px"><el-button type="primary" @click="preLook">预览结果</el-button></div>
          <div style="margin-right: 10px"><el-button type="primary" @click="exportXmindFile">导出 Xmind </el-button> </div>

        </div>
        <div style="padding: 0 10px 10px">
          背景色：<el-color-picker v-model="style.background" size="small"></el-color-picker>&nbsp;
          文字颜色：<el-color-picker v-model="style.color" size="small"></el-color-picker>&nbsp;
        </div>
        <div style="height: 600px;width: 100%;">
          <vue3-tree-org :data="xminddata || ''" :node-draggable="true"
                         :props="{ id: 'id', label: 'label', children: 'children' }" center :horizontal="horizontal"
                         :disabled="disaled" :collapsable="collapsable" :label-style="style" :only-one-node="onlyOneNode"
                         :clone-node-drag="cloneNodeDrag" :before-drag-end="beforeDragEnd"
                         :toolBar="{ scale: false, restore: false, expand: false, zoom: false, fullscreen: false, center: true }"
                         @on-node-drag="nodeDragMove" @on-node-drag-end="nodeDragEnd" @on-contextmenu="onMenus" @on-expand="onExpand"
                         @on-node-dblclick="onNodeDblclick" @on-node-click="onNodeClick" />
        </div>
      </div>



    </div>

  </div>


</template>
<script>
import { ElSwitch, ElColorPicker, ElMessage } from 'element-plus'
import { ref, getCurrentInstance } from 'vue'
import {apiAcceptData, downloadXmindFile} from "@/api";
export default {
  name: "XmindIndex",
  components: {
    ElSwitch,
    ElColorPicker,
  },
  setup() {
    const cloneNodeDrag = ref(true)
    const { proxy } = getCurrentInstance()
    return {
      cloneNodeDrag,
      proxy
    }
  },
  data() {
    return {
      textarea:"",
      xminddata: {},
      horizontal: true,
      collapsable: false,
      onlyOneNode: true,
      expandAll: true,
      disaled: true,
      style: {
        width:"200px",
        background: "#fff",
        color: "#2062c0",
      },
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptName: undefined,
        status: undefined
      }
    };
  },
  created() {
    this.toggleExpand(this.xminddata, this.expandAll);
  },
  methods: {
    async preLook(){
      var that  =this
      if(undefined == that.textarea || "" == that.textarea){
        that.$message.error("请输入内容");
        return;
      }
      const params = {
        textarea:that.textarea
      }
      const apiResult = await apiAcceptData('/xmind/textToData',params);
      this.xminddata = JSON.parse(apiResult)

    },
    async exportXmindFile(){
      var that  =this
      if(undefined == that.textarea || "" == that.textarea){
        that.$message.error("请输入内容");
        return;
      }
      const params = {
        textarea:that.textarea
      }
       await downloadXmindFile('/xmind/exportXmind',params,'abc.xmind');

    },
    onMenus({ node, command }) {
      console.log(node, command);
    },
    onExpand(e, data) {
      console.log(e, data);
    },
    onExpandAll(b) {
      console.log(b)
    },
    nodeDragMove(data) {
      console.log(data);
    },
    beforeDragEnd(node, targetNode) {
      return new Promise((resolve, reject) => {
        if (!targetNode) reject()
        if (node.id === targetNode.id) {
          reject()
        } else {
          resolve()
        }
      })
    },
    nodeDragEnd(data, isSelf) {
      console.log(data, isSelf);
    },
    onNodeDblclick() {
      console.log('onNodeDblclick')
    },
    onNodeClick(e, data) {
      ElMessage.info(data.deptName);
    },
    expandChange() {
      this.toggleExpand(this.data, this.expandAll);
    },
    toggleExpand(data, val) {
      if (Array.isArray(data)) {
        data.forEach((item) => {
          item.expand = val
          if (item.children) {
            this.toggleExpand(item.children, val);
          }
        });
      } else {
        data.expand = val
        if (data.children) {
          this.toggleExpand(data.children, val);
        }
      }
    }
  },
};
</script>

<style scoped lang="less">
.el-textarea {
  height: 590px;
  :deep(.el-textarea__inner) {
    height: 590px;
  }
}
</style>
